import away3d.core.base.*;

import com.powerfl.frm.taf.*;

import flash.display.*;
import flash.events.*;
import flash.external.*;
import flash.filters.*;
import flash.geom.*;
import flash.system.*;
import flash.text.*;

private function init( $e:Event ):void{
	var i:int, j:int, temp:Array, ini:Object, initScript:Array;
	removeEventListener( $e.type, arguments.callee );
	
	if( INITED === false ){
		INITED = true;
		System.useCodePage = false;
		STAGE = stage;
		SELF = this;
		
		stage.removeChildAt(0);
		preload( STAGE );
		
		STAGE.align = StageAlign.TOP_LEFT;
		STAGE.scaleMode = StageScaleMode.NO_SCALE;
		STAGE.quality = StageQuality.HIGH;
		STAGE.frameRate = 110;
		
		STAGE.showDefaultContextMenu = false;
		STAGE.stageFocusRect = false;
		STAGE.tabChildren = false;
		STAGE.doubleClickEnabled = false;
		
		airInit();
		poolInit();
		mathInit();
		runnerInit();
		keyInit();
		orderInit();
		tagInit();
		treeInit();
		flowInit();
		bindInit();
		runnerInit();
		
		addTree( '@DATA', '@@flashVar', 'data', STAGE.loaderInfo.parameters, null );
		STAGE.addEventListener( Event.ACTIVATE, SELF.ACTIVATE );
		STAGE.addEventListener( Event.DEACTIVATE, SELF.DEACTIVATE );
		STAGE.addEventListener(Event.MOUSE_LEAVE, SELF.MOUSE_LEAVE );
		STAGE.addEventListener(Event.RESIZE, SELF.RESIZE );
		STAGE.addEventListener( FullScreenEvent.FULL_SCREEN, SELF.FULL_SCREEN );
		STAGE.addEventListener( Event.ENTER_FRAME, SELF.ENTER_FRAME );
		STAGE.addEventListener( KeyboardEvent.KEY_DOWN, SELF.KEY_DOWN );
		STAGE.addEventListener( KeyboardEvent.KEY_UP, SELF.KEY_UP );
		STAGE.addEventListener( MouseEvent.MOUSE_DOWN, SELF.DOWN );
		STAGE.addEventListener( MouseEvent.MOUSE_OUT, SELF.UP );
		
		//초기화 인자가 설정 시
		if( flashVar( '@init' ) ){
			temp = flashVar( '@init' ).split( ',' );
		}else{
			temp = [];
		}
		if( _initArg ){
			for( i = 0, j = _initArg.length ; i < j ; ++i ){
				if( _initArg[i] is String ){
					temp[temp.length] = _initArg[i];
				}else if( _initArg[i] is Array ){
					airWindowSet.apply( null, _initArg[i] ); 
				}else{
					flowAdd( new _initArg[i] );
				}
			}
		}
		//설정파일이 지정된 경우
		if( temp.length ){
			//개별 설정파일을 로딩한다.
			for( i = 0, j = temp.length ; i < j ; ++i ){
				addText( '$iniText' + i.toString(), temp[i], 'remove' );
			}
			//파일을 병합하여 ini생성
			orderAdd( null, function( $iniCount:int ):void{
				var i:int, txt:String;
				txt = '';
				for( i = 0 ; i < $iniCount ; ++i ){
					txt += tdata( '$iniText' + i.toString() ) + '\r\n';
				}
				ini = INI( txt );
			}, j );
			//res파일 선로딩
			orderBranchAdd( function():void{
				var key:*;
				for( key in ini['@res'] ){
					addRes( key, ini['@res'][key] );
				}
			} );
			//res안의 ini검사 - @@ 규칙에 따라 검사
			orderBranchAdd( function():void{
				var i:int, txt:String, result:Object, key:*;
				txt = '';
				for( key in _trees ){
					if( _trees[key].type === 'TAFS' ){
						txt  += _trees[key].data + '\r\n';
						treeRemove_( key );
					}
				}
				if( txt ){
					result = INI( txt );
					for( key in result ){
						ini[key] = result[key]; 
					}
				}
			});
			//예약키워드처리
			orderBranchAdd( function():void{
				var key:*, key2:*, key3:*, temp:*, i:int, j:int, k:int, l:int;
				for( key in ini ){
					temp = ini[key.toLowerCase()];
					if( key === '@batch' ){
						for( key2 in temp ){
							flowAddMacro_( key2, scriptPre( temp[key2] ) );
						}
					}else if( key.substr( 0, 2 ) === '@@' ){
						flowAdd_( key.substr( 2 ), function( ...$param ):void{} );
					}
				}
				//내외장 메서드 검색테이블 생성
				for( key in ini ){
					temp = ini[key.toLowerCase()];
					switch( key.toLowerCase() ){
					case'@timeline':
						for( key2 in temp ){
							tweenLineData( key2, arrayCastNumber( arrayEvenIndex( temp[key2] ), false ), arrayOddIndex( temp[key2] ) );
						}
						break;
					case'@template':
						for( key2 in temp ){
							tdata( '@TEMPLATE' )[key2] = temp[key2];
						}
						break;
					case'@global':
						for( key2 in temp ){
							tdata( '@global' )[key2] = temp[key2];
						}
						break;
					case'@state':
						for( key2 in temp ){
							tdata( '@STATE' )[key2] = temp[key2];
							tdata( '@STATEVAR' )[key2] = temp[key2][0];
						}
						break;
					case'@bind':
						for( key2 in temp ){
							bindState_( key2, temp[key2] );
						}
						break;
					case'@sound':
						for( key2 in temp ){
							addSound( key2, temp[key2]  );
						}
						break;
					case'@image':
						for( key2 in temp ){
							addImage( key2, temp[key2] );
						}
						break;
					default:
						if( key.charAt(0) === '@' ){
							if( flowGet( key.substr(1) ) is Function ){
								addTree( '@local', '@' + key, 'data', ini[key], null );
							}else if( key.charAt(1) === '@' ){
								if( flowGet( key.substr(2) ) is Function ){
									addTree( '@script', '@' + key + '@script', 'data', scriptPre( ini[key] ), null );
								}
							}
						}
					}
				}
			} );
			//map처리
			orderBranchAdd( function():void{
				var key:*, key2:*, temp:*;
				for( key in ini ){
					if( _trees[key] && _trees[key].type === 'image' ){
						orderAdd( null, function( $key:* ):void{
							addMapEnd( tdata( $key ), ini[$key] );
						}, key );
					}
				}
				treeRemove( tagGet( 'remove' ) );
			} );
		}
		if( ExternalInterface.available && flashVar( 'focusName' ) ){
			ExternalInterface.call( 
				'focusing = function(){' +
				'var t = document.getElementsByName( "'+ flashVar( 'focusName' ) + '" );' +
				'if( t ) t.focus();'+
				'}' 
			);	
		}
		orderAdd( null, function():void{
			preloadRemove( STAGE );
			if( _trees['@@init@script'] ){
				script( _trees['@@init@script'].data );
			}
			_init();
			airWindowOpen();
		} );
	}else{
		preloadRemove( STAGE );
		_init();
	}
}
